const A = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    num: 1,
    allPrice: 0
  },
  pay() {
    let userId = wx.getStorageSync("userId");
    let config = {
      buyNum: this.data.num,
      goodId: this.data.goodId,
      payIntegral: this.data.allPrice,
      useFlag: 0,
      userId: userId
    }
    A.updata.appPay(config).then(res => {
      console.log(res);
      if(res.code == 200) {
        wx.redirectTo({
          url: `/pages/mall/orderList/orderList?type=`,
        })
      }else {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
      }
    })
  },

  // 减少商品数量
  minusCount() {
    let num = this.data.num;
    if (num <= 1) {
      wx.showToast({
        title: "亲,该宝贝不能再减少了~",
        icon: 'none'
      })
      return false;
    }
    num--;
    this.setData({
      num
    });
    this.getallprices();
  },

  // 增加商品数量
  addCount() {
    // 已选择数量
    let num = this.data.num;
    let integral = this.data.integral;
    // 商品总价
    let allPrice = this.data.allPrice;
    // 商品价格
    let price = this.data.price;
    allPrice = price * (num+1);
    if (allPrice > integral) {
      wx.showToast({
        title: "亲,该宝贝不能再多了~",
        icon: 'none'
      })
      return false;
    }
    num++;
    this.setData({
      num
    });
    this.getallprices();
  },

  // 计算总价格  所有选中商品的 （价格*数量）相加
  getallprices() {
    // 商品数量
    let num = this.data.num;
    // 商品总价
    let allPrice = this.data.allPrice;
    // 商品价格
    let price = this.data.price;
    allPrice = price * num;
    this.setData({
      allPrice: allPrice
    })
  },

  // 获取商品详情 
  getGoodsDetail() {
    let id = this.data.goodId;
    let config = {
      id
    }
    A.updata.getGoodsDetail(config).then(res => {
      console.log(res);
      this.setData({
        merchants: res.results.merchants
      })
    })
  },
  // 跳转商家详情
  toShopInfo(e) {
    let goodId = A.C(e).id;
    wx.navigateTo({
      url: `/pages/mall/goodsDetail/shopInfo/shopInfo?id=${goodId}`
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    let userInfo = JSON.parse(wx.getStorageSync('userInfo'));
    console.log(userInfo);
    this.setData({
      goodId: options.goodId,
      goodImg: options.goodImg,
      name: options.name,
      integral: userInfo.integral,
      userType: userInfo.userType
    })
    if (userInfo.userType == 1) {
      this.setData({
        price: Number(options.groupPrice)
      })
    }else {
      this.setData({
        price: Number(options.goodPrice)
      })
    }
    this.getallprices();
    this.getGoodsDetail();
  },
  

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})